//
//  JXPostsChildListCtl.m
//  JiaoX
//
//  Created by yinqy on 15/3/4.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXPostsChildListCtl.h"
#import "ForumDataTool.h"
#import "PostsItemFrame.h"
#import "PostsItemCell.h"
#import "MJRefresh.h"
#import "JXPostsDetailCtl.h"

@interface JXPostsChildListCtl ()
@property (nonatomic,assign)int modulePostsType;
@property (nonatomic,strong)BBSModel *model;
@property (nonatomic,strong)NSMutableArray *itemFrameArray;
@property (nonatomic,assign)int currentNum;
@property (nonatomic,assign)CGFloat lastScrollOffset;
@property (nonatomic,strong)NSArray *modelList;
@end


@implementation JXPostsChildListCtl

-(instancetype)initWithBBSModel:(BBSModel *)model modulePostsType:(int)type{
    self.modulePostsType = type;
    self.model = model;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _itemFrameArray = [NSMutableArray array];
    [self.tableView setTableFooterView:[[UIView alloc]init]];
    
    JXLog(@"self.view=%@",self.view);
    [self.tableView setSeparatorColor:SEPARATORCOLOR];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
    [self.tableView headerBeginRefreshing];
    
}

-(void)headerRefresh{
    self.currentNum = 1;
    [self requestDataWithLastId:@"" pageIndex:self.currentNum];
}

-(void)footerRefresh{
    if (_itemFrameArray.count) {
        PostsItemFrame *frame = [_itemFrameArray lastObject];
        [self requestDataWithLastId:frame.posts.postsId pageIndex:self.currentNum];
    }else{
        [self headerRefresh];
    }
}

-(void)requestDataWithLastId:(NSString *)lastId pageIndex:(int)pageIndex{
    [ForumDataTool getHomeDataWithModelId:self.model.modelId pageIndex:pageIndex lastId:lastId modulePostsType:self.modulePostsType success:^(BBSHomeData *result) {
        [Utils refreshComplete:self.tableView];
        if ([Utils isBlankString:result.data]) {
            if ([Utils isBlankString:lastId]) {
                [_itemFrameArray removeAllObjects];
            }
            [self convertDataToFrame:result];
        }else{
            [DialogUtils showTipsWithView:[[self.view superview] viewController].navigationController.view String:result.data];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTipsWithView:[[self.view superview] viewController].navigationController.view String:@"加载失败"];
    }];
}

-(void)convertDataToFrame:(BBSHomeData *)result{
    
    if (result.postsList!=nil &&result.postsList.count>0) {
        self.currentNum ++;
    }
    for (BBSPosts *post in result.postsList) {
        PostsItemFrame *frame = [[PostsItemFrame alloc]init];
        [frame setPosts:post];
        [_itemFrameArray addObject:frame];
    }
    if(result.modelList.count){
        self.modelList = result.modelList;
        for (BBSModel *model in self.modelList) {
            if([[NSString stringWithFormat:@"%@",model.modelId]isEqualToString:_model.modelId]){
                
            }
        }
    }
    [self.tableView reloadData];
}

-(void)viewDidLayoutSubviews
{
    [Utils setSeparatorInsetWithTableView:self.tableView insets:UIEdgeInsetsMake(0,JXPublicInset,0,0)];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _itemFrameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostsItemCell *cell = [PostsItemCell cellWithTableView:tableView];
    [cell setPostsItemFrame:_itemFrameArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    PostsItemFrame *frame = _itemFrameArray[indexPath.row];
    return frame.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PostsItemFrame *frame = _itemFrameArray[indexPath.row];
    JXPostsDetailCtl *ctl = [[JXPostsDetailCtl alloc]initWithBBSPosts:frame.posts];
    [[[self.view superview] viewController].navigationController pushViewController:ctl animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>scrollView.height) {
        if ([self.delegate respondsToSelector:@selector(scrollToSecondPage)]) {
            [self.delegate scrollToSecondPage];
        }
    }
    if (scrollView == self.tableView) {
        CGFloat y = scrollView.contentOffset.y;
        if(y<0 || y>scrollView.contentSize.height-self.tableView.height)return;
        if (y > self.lastScrollOffset) {
            //用户往上拖动
            [_delegate scrollUp];
        } else {
            //用户往下拖动
            [_delegate scrollDown];
        }
        self.lastScrollOffset = y;
    }
}

-(void)setBBSModel:(BBSModel *)model{
    self.model = model;
    [self.tableView headerBeginRefreshing];
}

@end
